package com.booking.service.impl;

import com.booking.common.dto.PageReq;
import com.booking.common.dto.PageResp;
import com.booking.common.enums.BookingErrorCode;
import com.booking.common.exception.BookingException;
import com.booking.dao.entity.AuditRecord;
import com.booking.dao.entity.RoomOrder;
import com.booking.dao.entity.ServiceOrder;
import com.booking.dao.service.AuditRecordService;
import com.booking.dao.service.RoomOrderService;
import com.booking.dao.service.ServiceOrderService;
import com.booking.enums.AuditStatusEnums;
import com.booking.enums.OrderStatusEnums;
import com.booking.service.AuditService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Qikai.Lin
 * @Date 2023/05/22
 */
@Service
@Slf4j
public class AuditServiceImpl implements AuditService {
    @Autowired
    private AuditRecordService auditRecordService;
    @Autowired
    private RoomOrderService roomOrderService;
    @Autowired
    private ServiceOrderService serviceOrderService;

    private RoomOrder getRoomOrder(Long orderId) {
        RoomOrder roomOrder = roomOrderService.getById(orderId);
        if (roomOrder == null) {
            throw new BookingException(BookingErrorCode.ORDER_NOT_EXIST);
        }
        return roomOrder;
    }

    private ServiceOrder getServiceOrder(Long orderId) {
        ServiceOrder serviceOrder = serviceOrderService.getById(orderId);
        if (serviceOrder == null) {
            throw new BookingException(BookingErrorCode.ORDER_NOT_EXIST);
        }
        return serviceOrder;
    }
    @Override
    public PageResp<AuditRecord> getAll(PageReq<Object> request) {
        PageResp<AuditRecord> page = auditRecordService.getPage(request);
        List<AuditRecord> auditRecords = page.getRecords();
//        // 只取待审核的
//        auditRecords = page.getRecords().stream().filter(record -> {
//            return Objects.equals(record.getAuditStatus(), AuditStatusEnums.READY_EXAMINE.getStatus());
//        }).collect(Collectors.toList());
        auditRecords.forEach(x -> {
            x.setAuditStatus(AuditStatusEnums.getMsgByStatus(x.getAuditStatus()));
        });
        page.setRecords(auditRecords);
        return page;
    }

    @Override
    public void shutdown(Long recordId) {
        log.info("审批记录修改:终止订单 recordId={}", recordId);
        AuditRecord auditRecord = auditRecordService.getById(recordId);
        auditRecord.setResult("终止订单");
        auditRecord.setAuditStatus(AuditStatusEnums.AUDITED.getStatus());
        auditRecordService.updateById(auditRecord);
    }

    @Override
    public void reject(Long recordId) {
        log.info("审批记录修改:拒绝受理 recordId={}", recordId);
        AuditRecord auditRecord = auditRecordService.getById(recordId);
        //修改订单状态
        if (auditRecord.getRoomOrderId() != null) {
            RoomOrder roomOrder = getRoomOrder(auditRecord.getRoomOrderId());
            roomOrder.setOrderStatus(OrderStatusEnums.END.getStatus());
            roomOrderService.updateById(roomOrder);
        } else if (auditRecord.getServiceOrderId() != null) {
            ServiceOrder serviceOrder = getServiceOrder(auditRecord.getServiceOrderId());
            serviceOrder.setOrderStatus(OrderStatusEnums.END.getStatus());
            serviceOrderService.updateById(serviceOrder);
        }

        auditRecord.setResult(AuditStatusEnums.REJECTED.getMsg());
        auditRecord.setAuditStatus(AuditStatusEnums.AUDITED.getStatus());
        auditRecordService.updateById(auditRecord);
    }
}
